package com.wechat.ferry.strategy.msg.receive.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dashscope.utils.JsonUtils;
import com.wechat.ferry.entity.dto.WxPpMsgDTO;
import com.wechat.ferry.entity.vo.request.WxPpWcfForWardMsgReq;
import com.wechat.ferry.entity.vo.request.WxPpWcfPassFriendApplyReq;
import com.wechat.ferry.enums.WcfMsgTypeEnum;
import com.wechat.ferry.service.WeChatDllService;
import com.wechat.ferry.strategy.msg.receive.ReceiveMsgStrategy;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 策略实现类-接收消息-签到处理
 *
 * @author chandler
 * @date 2024-12-25 14:19
 */
@Slf4j
@Component
public class SysMsgStrategyImpl implements ReceiveMsgStrategy {

    @Autowired
    private WeChatDllService weChatDllService;

    @Override
    public String getStrategyType() {
        return WcfMsgTypeEnum.RED_ENVELOPE_SYS_NOTICE.getCode();
    }

    @Override
    @SneakyThrows
    public String doHandle(WxPpMsgDTO dto) {
        log.info("执行系统消息,入参信息:{}", JsonUtils.toJson(dto));
//        if (ObjectUtil.isNotEmpty(dto) && dto.getContent().contains("以上是打招呼的内容")) {
//            //转发视频号消息
//            WxPpWcfForWardMsgReq wxPpWcfForWardMsgReq = new WxPpWcfForWardMsgReq();
//            wxPpWcfForWardMsgReq.setId(dto.getId());
//            wxPpWcfForWardMsgReq.setReceiver(dto.getSender());
//            wxPpWcfForWardMsgReq.setId(2262003095106675948L);
//            wxPpWcfForWardMsgReq.setReceiver("wxid_gavfaz6erooq22");
//            weChatDllService.forward(wxPpWcfForWardMsgReq);
//        }
        return "";
    }


    public static Map<String, Object> parseXmlToMap(String xml) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new java.io.ByteArrayInputStream(xml.getBytes()));

        Element root = document.getDocumentElement();
        return parseElement(root);
    }

    private static Map<String, Object> parseElement(Element element) {
        Map<String, Object> map = new HashMap<>();
        NamedNodeMap attributes = element.getAttributes();

        // 解析属性
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);
            map.put(attribute.getNodeName(), attribute.getNodeValue());
        }

        // 解析子节点
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element childElement = (Element) node;
                String key = childElement.getNodeName();
                Map<String, Object> childMap = parseElement(childElement);

                // 如果 key 已存在，则存储为列表
                if (map.containsKey(key)) {
                    Object existingValue = map.get(key);
                    if (existingValue instanceof List) {
                        ((List<Map<String, Object>>) existingValue).add(childMap);
                    } else {
                        List<Map<String, Object>> list = new java.util.ArrayList<>();
                        list.add((Map<String, Object>) existingValue);
                        list.add(childMap);
                        map.put(key, list);
                    }
                } else {
                    map.put(key, childMap);
                }
            } else if (node.getNodeType() == Node.TEXT_NODE && !node.getTextContent().trim().isEmpty()) {
                map.put("value", node.getTextContent().trim());
            }
        }

        return map;
    }
}
